package com.towa.ninemanmorris;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseToWa {

	private DatabaseHelper dbHelper;
	private SQLiteDatabase database;
	private static int redMarker = 0;
	private static int blueMarker = 1;

	public DatabaseToWa(Context context) {
		dbHelper = new DatabaseHelper(context, "towaninemanmorris.db");
		database = dbHelper.getWritableDatabase();
//		if(loadGame(1) == null)
//			saveGame(new GameStatusWrapper());
//		updateGame(new GameStatusWrapper());
//		if(getGameList().size() == 0)
//			saveGame(new GameStatusWrapper());
	}

	public synchronized void close() {
		dbHelper.close();
	}

	public synchronized void updateGame(GameStatusWrapper gsw) {
		database.beginTransaction();
		ContentValues values = new ContentValues();
//		values.put("_id", gsw.getId());
		values.put("gamename", gsw.getGameName());
		values.put("player", gsw.getPlayer());
		values.put("redsonboard", gsw.getRedsOnBoard());
		values.put("bluesonboard", gsw.getBluesOnBoard());
		values.put("redsplaced", gsw.getRedsPlaced());
		values.put("bluesplaced", gsw.getBluesPlaced());

		database.update("gamestatus", values, "_id = 1", null);

		int[] redMarkers = gsw.getRedMarkers();
		for (int i = 0; i < redMarkers.length; i++) {
			values.clear();
			values.put("color", redMarker);
			values.put("position", redMarkers[i]);
			values.put("gameid", 1);
//			database.insert("markerposition", null, values);
			database.update("markerPosition", values, "_id = " + (i + 1), null);
		}
		int[] blueMarkers = gsw.getBlueMarkers();
		for (int i = 0; i < blueMarkers.length; i++) {
			values.clear();
			values.put("color", blueMarker);
			values.put("position", blueMarkers[i]);
			values.put("gameid", 1);
//			database.insert("markerposition", null, values);
			database.update("markerPosition", values, "_id = " + (i + 10), null);
		}
		database.setTransactionSuccessful();
		database.endTransaction();
	}

	public synchronized long saveGame(GameStatusWrapper gsw) {
		database.beginTransaction();
		ContentValues values = new ContentValues();
		values.put("gamename", gsw.getGameName());
		values.put("player", gsw.getPlayer());
		values.put("redsonboard", gsw.getRedsOnBoard());
		values.put("bluesonboard", gsw.getBluesOnBoard());
		values.put("redsplaced", gsw.getRedsPlaced());
		values.put("bluesplaced", gsw.getBluesPlaced());
		long id = database.insert("gamestatus", null, values);
		if (id > -1) {
			int[] redMarkers = gsw.getRedMarkers();
			for (int i = 0; i < redMarkers.length; i++) {
				values.clear();
				values.put("color", redMarker);
				values.put("position", redMarkers[i]);
				values.put("gameid", id);
				database.insert("markerposition", null, values);
			}
			int[] blueMarkers = gsw.getBlueMarkers();
			for (int i = 0; i < blueMarkers.length; i++) {
				values.clear();
				values.put("color", blueMarker);
				values.put("position", blueMarkers[i]);
				values.put("gameid", id);
				database.insert("markerposition", null, values);
			}
			database.setTransactionSuccessful();
		}
		database.endTransaction();
		return id;
	}

	public synchronized GameStatusWrapper loadGame(long id) {
		GameStatusWrapper gsw = new GameStatusWrapper();
		Cursor cursor = database.query("gamestatus", null, "_id = " + id, null,
				null, null, null);
		if (cursor.moveToFirst()) {
			gsw.setId(id);
			gsw.setGameName(cursor.getString(1));
			gsw.setPlayer(cursor.getInt(2));
			gsw.setRedsOnBoard(cursor.getInt(3));
			gsw.setBluesOnBoard(cursor.getInt(4));
			gsw.setRedsPlaced(cursor.getInt(5));
			gsw.setBluesPlaced(cursor.getInt(6));
			cursor.close();

			cursor = database.query("markerposition", new String[] { "color",
					"position" }, "gameid = " + id, null, null, null, null);
			if (cursor.moveToFirst()) {
				int[] redMarkers = new int[9];
				int[] blueMarkers = new int[9];
				int redi = 0;
				int bluei = 0;
				int color;
				int pos;
				do {
					color = cursor.getInt(0);
					pos = cursor.getInt(1);
					if (color == redMarker) {
						redMarkers[redi] = pos;
						redi++;
					} else {
						blueMarkers[bluei] = pos;
						bluei++;
					}
				} while (cursor.moveToNext());
				gsw.setRedMarkers(redMarkers);
				gsw.setBlueMarkers(blueMarkers);
				cursor.close();
			}
			return gsw;
		}
		return null;
	}

	public synchronized List<GameNameIdWrapper> getGameList() {
		List<GameNameIdWrapper> list = new ArrayList<GameNameIdWrapper>();
		Cursor cursor = database.query("gamestatus", new String[] { "_id",
				"gamename" }, null, null, null, null, "gamename desc");

		if (cursor.moveToFirst()) {
			do {
				list.add(new GameNameIdWrapper(cursor.getLong(0), cursor
						.getString(1)));
			} while (cursor.moveToNext());
		}
		cursor.close();
		return list;
	}

	private class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context, String databaseName) {
			super(context, databaseName, null, 1);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("create table gamestatus (_id integer primary key autoincrement, gamename text not null,"
					+ "player integer, redsonboard integer, bluesonboard integer,"
					+ "redsplaced integer, bluesplaced integer)");
			db.execSQL("create table markerposition (_id integer primary key autoincrement, color integer,"
					+ "position integer, gameid integer)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

		}

	}
}
